package com.zmj.framework.dao.jdbc;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.zmj.framework.bean.Log;
import com.zmj.framework.dao.LogDao;
import com.zmj.framework.util.PageController;
@Repository
public class LogDaoJdbc implements LogDao {
	@Autowired
	FrmJdbcTemplate jdbcTemplate;
	public List getLogList(Log log, PageController controller) {
		String tmpSql = "1=1";
		if ((log.getKssj() != null) && (!(log.getKssj().equals("")))) {
			tmpSql += " and czsj>='" + log.getKssj() + "' ";
		}
		if ((log.getJssj() != null) && (!(log.getJssj().equals("")))) {
			tmpSql += " and czsj<='" + log.getJssj() + "' ";
		}
		if ((log.getCzlx() != null) && (!(log.getCzlx().equals("")))) {
			tmpSql += " and czlx like '%" + log.getCzlx() + "%' ";
		}
		if ((log.getYhmc() != null) && (!(log.getYhmc().equals("")))) {
			tmpSql += " and yhmc like '%" + log.getYhmc() + "%' ";
		}
		tmpSql = "select yhmc,DATE_FORMAT(czsj,'%Y-%m-%d %H:%i:%S') czsj,czlx,cznr,ip,bz from frm_log where " + tmpSql + "";
		FrmRowMapper mapper = new FrmRowMapper(Log.class);
		return controller.getWarpedList(tmpSql, mapper, this.jdbcTemplate);
	}

	public int saveLog(Log log) {
		int 	ret=jdbcTemplate.update("insert into frm_log(yhmc,czsj,czlx,cznr,ip,bz) values(" +
				"'" +log.getYhmc()+"', " +
				"now(), " +
				"'" +log.getCzlx()+"', "+
				"'" +log.getCznr()+"', "+
				"'" +log.getIp()+"', "+
				"'" +log.getBz()+"'"+
				")");
		return ret;
	}

	public Log getLog(String id) {
		String tmpSql = "select yhmc,DATE_FORMAT(czsj,'%Y-%m-%d') czsj,czlx,cznr,ip,bz from frm_log where id=" + id ;
		List list = this.jdbcTemplate.queryForList(tmpSql, Log.class);
		if ((list == null) || (list.size() == 0)) {
			return null;
		}
		return ((Log) list.get(0));
	}

}
